//
//  OrdinaryFeedVController.m
//  优学荟管理系统
//
//  Created by edz on 2018/2/27.
//  Copyright © 2018年 星道三好. All rights reserved.
//

#import "OrdinaryFeedVController.h"
#import "OrdinarSheetCell.h"
@interface OrdinaryFeedVController ()<UITableViewDelegate,UITableViewDataSource,CustomAlertDelegete>
{
    NSInteger _mark;//标记类型
    NSString *_selectDate;//标记选中的日期
}
@property(nonatomic,strong)NSArray *dataArr;
@property(nonatomic,strong)UIButton *starButton;
@property(nonatomic,strong)UIButton *endButton;
@property (nonatomic,strong) CustomDayDatePicker *datePicker;
@end

@implementation OrdinaryFeedVController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"反馈列表";
    [self setUI];
}
-(NSArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSArray array];
    }
    
    return _dataArr;
}
-(void)setUI
{
    UITableView *tableView = [[UITableView alloc]init];
    [self.view addSubview:tableView];
    tableView.delegate=self;
    tableView.dataSource=self;
  [tableView setTableHeaderView:[self getHeaderView]];
    [tableView registerClass:[OrdinarSheetCell class] forCellReuseIdentifier:@"Cell"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return  self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrdinarSheetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    return cell;
}
-(void)btnindex:(int)index :(int)tag
{
    if (index == 2) {
        if (_mark == 1) {
            _selectDate = [NSString stringWithFormat:@"%d-%02d-%02d",self.datePicker.year,self.datePicker.month,self.datePicker.day];
            [self.starButton setTitle:[NSString stringWithFormat:@"%@",_selectDate] forState:(UIControlStateNormal)];
        }else if (_mark == 2){
            
            _selectDate = [NSString stringWithFormat:@"%d-%02d-%02d",self.datePicker.year,self.datePicker.month,self.datePicker.day];
            [self.endButton setTitle:[NSString stringWithFormat:@"%@",_selectDate] forState:(UIControlStateNormal)];
            
            [self sureButtonAction];
        }
    }
}
-(UIView *)getHeaderView
{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 110)];
    headView.backgroundColor=RGBColor(245, 245, 245);
    
    UILabel *lab = [[UILabel alloc]init];
    [headView addSubview:lab];
    lab.text=@"选择时间";
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(26);
        make.top.offset(20);
    }];
    
    _starButton = [[UIButton alloc]init];
    [headView addSubview:_starButton];
    [self.starButton setTitle:[NSString getCurrentDateYearMD] forState:(UIControlStateNormal)];
    _starButton.backgroundColor=[UIColor whiteColor];
    [_starButton setFont:[UIFont systemFontOfSize:13]];
    [_starButton setTitleColor:RGBColor(102, 102, 102) forState:0];
    [_starButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.top.equalTo(lab.mas_bottom).offset(14);
        make.width.offset(kARWidthTranslator(60));
        make.height.offset(30);
    }];
    
    _endButton = [[UIButton alloc]init];
    [headView addSubview:_endButton];
    [self.endButton setTitle:[NSString currentDateTow] forState:(UIControlStateNormal)];
    [_endButton setFont:[UIFont systemFontOfSize:13]];
    _endButton.backgroundColor=[UIColor whiteColor];
    [_endButton setTitleColor:RGBColor(102, 102, 102) forState:0];
    [_endButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_starButton.mas_right).offset(20);
        make.top.equalTo(lab.mas_bottom).offset(14);
        make.width.offset(kARWidthTranslator(60));
        make.height.offset(30);
    }];
    self.starButton.tag = 201;
    self.endButton.tag = 202;
    [self.starButton addTarget:self action:@selector(allButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.endButton addTarget:self action:@selector(allButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIButton *sureBtn = [[UIButton alloc]init];
    [headView addSubview:sureBtn];
    [sureBtn setTitle:@"确定" forState:0];
    [sureBtn setBackgroundImage:[UIImage imageNamed:@"btn_ensure_default"] forState:0];
    [sureBtn addTarget:self action:@selector(sureButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.endButton.mas_right).offset(20);
        make.top.equalTo(lab.mas_bottom).offset(14);
        make.width.offset(60);
        make.height.offset(30);
    }];
    
    return headView;
}
- (CustomDayDatePicker *)datePicker {
    
    if (_datePicker == nil) {
        self.datePicker = [[CustomDayDatePicker alloc]initWithFrame:CGRectMake(0, 20, kScreenWidth - 60, 140)];
        
    }
    return _datePicker;
}
- (void)allButtonAction:(UIButton *)sender{
    
    NSInteger tag = sender.tag;
    
    
    if (201 == tag) {
        JXAlertview *alert = [[JXAlertview alloc]initWithFrame:CGRectMake(10, 0, kScreenWidth - 60, 200)];
        alert.center = self.view.center;
        alert.delegate = self;
        [alert initwithtitle:@"开始时间" andcommitbtn:@"确定" andStr:@"0"];
        [alert addSubview:self.datePicker];
        [alert show];
        
        
        _mark = 1;
        
    }else{
        
        JXAlertview *alert = [[JXAlertview alloc]initWithFrame:CGRectMake(10, 0, kScreenWidth - 60, 200)];
        alert.center = self.view.center;
        alert.delegate = self;
        [alert initwithtitle:@"结束时间" andcommitbtn:@"确定" andStr:@"0"];
        [alert addSubview:self.datePicker];
        [alert show];
        
        
        _mark = 2;
        
    }
    
    
    
    
}
#pragma mark --- 确定方法的实现
- (void)sureButtonAction{
    
    NSString *start = [self.starButton.currentTitle stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    NSString *endSelect = [self.endButton.currentTitle stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    
    NSComparisonResult result = [start compare:endSelect];
    
    if (result != NSOrderedAscending) {
        [SVProgressHUD showErrorWithStatus:@"结束时间必须大于开始时间!"];
    }else{
        
        
        [self setSetTowSureStart:start endSelect:endSelect];
        
        
        
    }
    
    
    
}
- (void)setSetTowSureStart:(NSString *)startStr endSelect:(NSString *)endSelect{
    
    
    [self.starButton setTitle:startStr forState:(UIControlStateNormal)];
    [self.endButton setTitle:endSelect forState:(UIControlStateNormal)];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
